﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using QD.LopPlat.UI.Code;
using QD.LopPlat.UI.Code.DbContent;
using QD.Tools;

namespace QD.LopPlat.UI
{
    public delegate void AddNewUser(string[] userlist);

    public partial class Sys_UidPwdQfAdd : BaseForm
    {
        public event AddNewUser SureEvent;

        public Sys_UidPwdQfAdd()
        {
            InitializeComponent();

            this.grid.InitGrid();

            DataGridViewCheckBoxHeaderCell checkBoxHeaderCell = new DataGridViewCheckBoxHeaderCell();
            checkBoxHeaderCell.OnCheckBoxClicked += checkBoxHeaderCell_OnCheckBoxClicked;
            DataGridViewCheckBoxColumn checkColumn = (DataGridViewCheckBoxColumn)this.grid.Columns["IsChecked"];
            checkColumn.HeaderCell = checkBoxHeaderCell;
            checkColumn.HeaderCell.Value = string.Empty;
        }

        void checkBoxHeaderCell_OnCheckBoxClicked(object sender, datagridviewCheckboxHeaderEventArgs e)
        {
            for (int i = 0; i < grid.Rows.Count; i++)
            {
                grid.Rows[i].Cells[0].Value = e.CheckedState.ToString();
            }
        }

        private void Sys_UidPwdQfAdd_Load(object sender, EventArgs e)
        {
            Bind_Data();
        }

        private void Bind_Data()
        {
            string key = this.txtKey.Text.Trim();

            DbQfDevPlat dbPlat = new DbQfDevPlat();
            string sql = string.Format("select a.* from Sys_UidPwd a  where not exists(select * from Sys_UidRefQf b where a.Uid = b.Uid and b.QfCode = '{0}')", QDDataBaseHelper.CurQDDataBase.QfCode);

            if (!string.IsNullOrEmpty(key))
            {
                sql += string.Format(" and (uid like '%{0}%' or UserName like '%{0}%')", key);
            }

            DataTable dt = dbPlat.Query(sql);
            dt.AddColumn("IsChecked", typeof(bool));
            this.grid.DataSource = dt;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)this.grid.DataSource;
            DataRow[] dt_checked = dt.Select("IsChecked=true");

            if (dt_checked.Length == 0)
            {
                Tools.PromptHelper.Prompt("请选择需要添加的用户！");
            }
            else
            {
                string[] userlist = new string[dt_checked.Length];

                for (int i = 0; i < dt_checked.Length; i++)
                {
                    userlist[i] = dt_checked[i]["Uid"].ToString();
                }

                if (SureEvent != null)
                {
                    SureEvent(userlist);
                }

                this.Close();
            }
        }

        private void txtKey_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Bind_Data();
            }
        }
    }
}
